<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LaborContract extends Model
{
    use HasFactory;
    protected $fillable = [
        'team_id',
        'user_id',
        'project_id',
        'labor_id',
        'type',
        'price',
            'tax_rate','tax_price',
            'name',
            'tax_id',
            'address',
            'mobile',
            'bank',
            'bank_number',
            'bank_account',
            'email',
            'postal_code',

            'b_name',
            'b_tax_id',
            'b_address',
            'b_mobile',
            'b_bank',
            'b_bank_number',
            'b_bank_account',
            'b_email',
            'b_postal_code',
            'entrusted_agent',
            'agent',
            'contract_canned',

            'u_name',
            'u_idard',
            'u_address',
            'u_mobile',
            'u_contacts',
            'u_contacts_mobile',
            'u_idard_file',
            'u_work',
            'u_email',
            'u_contract_canned',

            'build_address',
            'craft',
            'receiver',
            'sums',
            'unit_price',
            'pay_type',
            'apply_id',
            'liquidated_damages',
            'approver',
            'remarks'
    ];

    const POSTONLY = [
        'team_id',
        'user_id',
        'project_id',
        'labor_id',
        'type',
        'price',
            'tax_rate','tax_price',
            'name',
            'tax_id',
            'address',
            'mobile',
            'bank',
            'bank_number',
            'bank_account',
            'email',
            'postal_code',

            'b_name',
            'b_tax_id',
            'b_address',
            'b_mobile',
            'b_bank',
            'b_bank_number',
            'b_bank_account',
            'b_email',
            'b_postal_code',
            'entrusted_agent',
            'agent',
            'contract_canned',

            'u_name',
            'u_idard',
            'u_address',
            'u_mobile',
            'u_contacts',
            'u_contacts_mobile',
            'u_idard_file',
            'u_work',
            'u_email',
            'u_contract_canned',

            'build_address',
            'craft',
            'receiver',
            'sums',
            'unit_price',
            'pay_type',
            'apply_id',
            'liquidated_damages',
            'approver',
            'remarks'
    ];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }


    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function person()
    {
        return $this->hasOne(User::class,'id','person_id')->select('id','name');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->where('model','LaborContract')->orderBy('id','desc');
    }

    public function reimburses()
    {
        return $this->hasMany(Reimburse::class,'source_id','id')->with('applicationApproved');
    }

    public function details()
    {
        return $this->hasMany(LaborContractDetail::class,'labor_contract_id','id');
    }
}
